<?php

/**
 * This is the model class for table "questions".
 *
 * The followings are the available columns in table 'questions':
 * @property integer $question_id
 * @property string $questions_title
 * @property integer $questions_type
 * @property string $questions_content
 * @property string $questions_answer
 * @property string $questions_explanation
 * @property integer $question_creater_id
 * @property string $questions_create_time
 * @property string $questions_edit_time
 */
class Questions extends CActiveRecord
{
	/**
	 * 单选 多选 不定项 判断
	 * @var unknown_type
	 */
	const QTypeDanxuan = 1;
	const QTypeDuoxuan = 2;
	const QTypeBuDing = 3;
	const QTypePanduan =4;
	/**
	 * 返回题型数组
	 * @return array 
	 */
	public function getQTypeOption() {
		return array(
			self::QTypeDanxuan => '单选题',
			self::QTypeDuoxuan => '多选题',
			self::QTypeBuDing => '不定项题',
			self::QTypePanduan => '判断题',
		);
	}
	/**
	 * 返回题型名称
	 */
	public function getQTyptText($type="") {
		if($type != ""){
			$this->questions_type = $type;
		}
		$options = $this->getQTypeOption();
		return isset($options[$this->questions_type])?$options[$this->questions_type]:'未定义题型';
	}
	/**
	 * Returns the static model of the specified AR class.
	 * @return Questions the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'questions';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('questions_title,questions_type,questions_content, questions_answer, questions_explanation', 'required'),
			array('questions_type', 'in', 'range'=>array(1,2,3,4)),
			array('questions_title', 'length', 'max'=>255),
			array('questions_content, questions_answer, questions_explanation', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('question_id, questions_title, questions_type, questions_content, questions_answer, questions_explanation, question_creater_id, questions_create_time, questions_edit_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'question_creater' => array(self::BELONGS_TO, 'User', 'question_creater_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'question_id' => '试题ID',
			'questions_title' => '试题标题',
			'questions_type' => '试题题型',
			'questions_content' => '内容',
			'questions_answer' => '答案',
			'questions_explanation' => '解析',
			'question_creater_id'=> '创建人',
			'questions_create_time'=> '创建时间',
			'questions_edit_time'=> '修改时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('question_id',$this->question_id);

		$criteria->compare('questions_title',$this->questions_title,true);

		$criteria->compare('questions_type',$this->questions_type);

		$criteria->compare('questions_content',$this->questions_content,true);

		$criteria->compare('questions_answer',$this->questions_answer,true);

		$criteria->compare('questions_explanation',$this->questions_explanation,true);

		$criteria->compare('question_creater_id',$this->question_creater_id);

		$criteria->compare('questions_create_time',$this->questions_create_time,true);

		$criteria->compare('questions_edit_time',$this->questions_edit_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function beforeSave() {
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->question_creater_id = Yii::app()->user->id;
				$this->questions_create_time = $this->questions_edit_time = date("Y-m-d H:i:s");
			}else{
				$this->questions_edit_time = date("Y-m-d H:i:s");
			}
			return true;
		}else{
			return false;
		};
	}
	public function filterData()
	{
		if(Yii::app()->user->getState('roles') != 0)
		{
			$this->question_creater_id = Yii::app()->user->id;
		}
	}
}